//
//  CWCWeatherViewController.m
//  ChuanNews
//
//  Created by dllo on 16/4/27.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "CWCWeatherViewController.h"
#import "UIImageView+WebCache.h"
#import "BaseHeader.h"
@interface CWCWeatherViewController ()
@property(nonatomic, retain)UIImageView *bigImg;
@property(nonatomic, retain)UILabel *addressLabel;
@property(nonatomic, retain)UIImageView *weatherLocation;
@property(nonatomic, retain)UILabel *dateWeekLabel;
@property(nonatomic, retain)UIImageView *weatherImg;
@property(nonatomic, retain)UILabel *temperatureLabel;
@property(nonatomic, retain)UILabel *climateLabel;
@property(nonatomic, retain)UILabel *windLabel;
@property(nonatomic, retain)UILabel *pm2d5Label;
@property(nonatomic, retain)UILabel *sourceLabel;
@end

@implementation CWCWeatherViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.title = @"天气";
    [self creatData];
    [self creatView];
    
}
-(void)creatData{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    [manager GET:@"http://c.3g.163.com/nc/weather/6L695a6BfOWkp%2Bi%2Fng%3D%3D.html" parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = responseObject;
        NSArray *array = dic[@"辽宁|大连"];
        NSDictionary *dict = array[0];
        NSDictionary *dictionary = dic[@"pm2d5"];
        self.modelDetail = [[CWCWeatherDetailModel alloc]initWithDic:dic];
        self.model = [[CWCWeatherModel alloc]initWithDic:dict];
        self.modelPM2d5 = [[CWCPM2Model alloc]initWithDic:dictionary];
        [self addWeather];
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
        
    }];
    
}

-(void)addWeather{
    
    NSMutableString *temp = [self.model.temperature mutableCopy];
    [temp replaceOccurrencesOfString:@"C" withString:@"" options:NSCaseInsensitiveSearch range:NSMakeRange(0, temp.length)];
    self.temperatureLabel.text = temp;
    NSString *dataString = [self.modelDetail.dt substringFromIndex:5];
    self.dateWeekLabel.text = [NSString stringWithFormat:@"%@  %@",dataString , self.model.week];
    NSString *airZhiliang;
    int pm = self.modelPM2d5.pm2_5.intValue;
    if (pm < 50) {
        airZhiliang = @"优";
    }else if (pm < 100){
        airZhiliang = @"良";
    }else if (pm < 150){
        airZhiliang = @"轻度污染";
    }else if (pm < 200){
        airZhiliang = @"中度污染";
    }else if (pm < 300){
        airZhiliang = @"重度污染";
    }else if (pm >= 300){
        airZhiliang = @"严重污染";
    }
    self.pm2d5Label.text = [NSString stringWithFormat:@"PM2.5  %d  %@", pm, airZhiliang];
    self.climateLabel.text = self.model.climate;
    self.windLabel.text = self.model.wind;
    [self.bigImg sd_setImageWithURL:[NSURL URLWithString:self.modelPM2d5.nbg2]];
    if ([self.model.climate isEqualToString:@"暴风雨"]) {
        self.weatherImg.image = [UIImage imageNamed:@"thunderhailstone@2x.png"];
    }else if ([self.model.climate isEqualToString:@"晴"]) {
        self.weatherImg.image = [UIImage imageNamed:@"sun@2x.png"];
    }else if ([self.model.climate isEqualToString:@"多云"]) {
        self.weatherImg.image = [UIImage imageNamed:@"sun_and_cloud_mini@2x.png"];
    }else if ([self.model.climate isEqualToString:@"阴"]) {
        self.weatherImg.image = [UIImage imageNamed:@"cloud@2x.png"];
    }else if ([self.model.climate isEqualToString:@"雨"]) {
        self.weatherImg.image = [UIImage imageNamed:@"rain.imageset/rain@2x.png"];
    }else if ([self.model.climate isEqualToString:@"雪"]) {
        self.weatherImg.image = [UIImage imageNamed:@"snow.imageset/snow@2x.png"];
    }else {
        self.weatherImg.image = [UIImage imageNamed:@"sand_float_mini@2x.png"];
    }
}

-(void)creatView{
    self.bigImg = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 64)];
    [self.view addSubview:self.bigImg];
    self.bigImg.backgroundColor = [UIColor blueColor];
    self.addressLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2 - 30, 40, 40, 24)];
    self.addressLabel.font = [UIFont systemFontOfSize:20];
    self.addressLabel.text = @"大连";
    self.addressLabel.textColor = [UIColor whiteColor];
    [self.bigImg addSubview:self.addressLabel];
    
    self.weatherLocation = [[UIImageView alloc]initWithFrame:CGRectMake(WIDTH / 2 + 10, 40, 24, 24)];
    self.weatherLocation.image = [UIImage imageNamed:@"map.png"];
    [self.bigImg addSubview:self.weatherLocation];
    self.dateWeekLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2 - 60, 70, 120, 25)];
    //self.dateWeekLabel.backgroundColor = [UIColor redColor];
    self.dateWeekLabel.textColor = [UIColor whiteColor];
    [self.bigImg addSubview:self.dateWeekLabel];
    
    self.weatherImg = [[UIImageView alloc]initWithFrame:CGRectMake(50, 155, 100, 150)];
    //self.weatherImg.backgroundColor = [UIColor grayColor];
    [self.bigImg addSubview:self.weatherImg];
    
    self.temperatureLabel = [[UILabel alloc]initWithFrame:CGRectMake(210, 155, 125, 40)];
    //self.temperatureLabel.backgroundColor = [UIColor yellowColor];
    self.temperatureLabel.textColor = [UIColor whiteColor];
    self.temperatureLabel.font = [UIFont systemFontOfSize:30];
    
    [self.bigImg addSubview:self.temperatureLabel];
    
    self.climateLabel = [[UILabel alloc]initWithFrame:CGRectMake(210, 200, 100, 30)];
    //self.climateLabel.backgroundColor = [UIColor orangeColor];
    self.climateLabel.font = [UIFont systemFontOfSize: 20];
    self.climateLabel.textColor = [UIColor whiteColor];
    [self.bigImg addSubview:self.climateLabel];
    
    self.windLabel = [[UILabel alloc]initWithFrame:CGRectMake(210, 235, 100, 30)];
    //self.windLabel.backgroundColor = [UIColor lightGrayColor];
    self.windLabel.font = [UIFont systemFontOfSize:20];
    self.windLabel.textColor = [UIColor whiteColor];
    [self.bigImg addSubview:self.windLabel];
    
    self.pm2d5Label = [[UILabel alloc]initWithFrame:CGRectMake(210, 270, 150, 30)];
    //self.pm2d5Label.backgroundColor = [UIColor cyanColor];
    self.pm2d5Label.font = [UIFont systemFontOfSize:20];
    self.pm2d5Label.textColor = [UIColor whiteColor];
    [self.bigImg addSubview:self.pm2d5Label];
    
    self.sourceLabel = [[UILabel alloc]initWithFrame:CGRectMake(WIDTH / 2 - 100, HEIGHT - 120, 200, 30)];
    self.sourceLabel.text = @"-数据来自中国天气-";
    self.sourceLabel.textAlignment = 1;
    self.sourceLabel.font = [UIFont systemFontOfSize:15];
    self.sourceLabel.textColor = [UIColor grayColor];
    [self.bigImg addSubview:self.sourceLabel];
    
    
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
